﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Requisitions;

namespace MyMES.Read.API.Application.Handler.Requisitions
{
    public class RequisitionHandler : IRequestHandler<RequisitionCommand, APIPaging<List<Requisition>>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Requisition> repository;
        /// <summary>
        /// 生产领料
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="repository"></param>
        public RequisitionHandler(IMapper mapper,IBaseRepository<Requisition> repository)
        {
            this.mapper = mapper;
            this.repository = repository;
        }
        /// <summary>
        /// 生产领料
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIPaging<List<Requisition>>> Handle(RequisitionCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<List<Requisition>> result = new APIPaging<List<Requisition>>();
                result.Code = APIEnum.查询成功;
                result.Msg = "显示成功";
                string sql = "select *  from Requisition  ORDER BY Requisitionid  DESC";
                var list = await repository.SelectAsync(sql);
                result.TotalCount = list.Count;
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.size);
                list = list.Skip((request.index - 1) * request.size).Take(request.size).ToList();
                result.data = list;
                return result;
            }
            catch (Exception)
            {

                throw;
            }
          
        }
    }
}
